<?php
/**
 * --------------------------------------------------------------
 * 手册控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\index\controller;

use app\common\controller\HomeBase;
use app\admin\model\Manual as ManualModel;
use app\admin\model\ManualTypes as ManualTypesModel;
use app\admin\model\ManualDoc as ManualDocModel;
use think\Response;

class Manual extends HomeBase
{
    /**
     * 首页
     */
    public function index()
    {
        $list = $this->_getDataList(0);
        
        $this->assign([
            'title' => '文档 - ' . config('app.site_setting.web_site_title'),
            'list'  => $list,
        ]);
        return $this->fetch();
    }

    /**
     * 列表
     */
    public function listing()
    {
		$id = input('id/d');
        
    	if ($id <= 0 || !is_numeric($id)) $this->error('参数错误');
        $map['status'] = 1;
        if (!empty($id)) {
            $map['id'] = $id;
        }
        $cinfo = ManualTypesModel::where($map)->field('id,title')->find();
        $list  = $this->_getDataList($cinfo['id']);

        $this->assign([
            'title' => $cinfo['title'] . ' - ' . config('app.site_setting.web_site_title'),
            'list'  => $list,
        ]);
        return $this->fetch('index');
    }

    /**
     * 通用分页数据获取
     * @param integer $type_id 分类ID
     */
    private function _getDataList($type_id)
    {
        $all_manual_types = cache('all_manual_types');
        if (empty($all_manual_types)) {
            $all_manual_types = ManualTypesModel::where(['status' => 1])->field('id,pid')->select();
            $all_manual_types = $all_manual_types ? object_to_array($all_manual_types) : null;
            // 非开发模式，缓存所有栏目id
            if (config('app.site_setting.develop_mode') == 0) {
                cache('all_manual_types', $all_manual_types);
            }
        }
        $sub_manual_types = get_all_subs($all_manual_types, $type_id);
        $all_type_ids     = array_column($sub_manual_types, 'id');
        if ($type_id > 0) $all_type_ids = array_merge($all_type_ids, [$type_id]);

        $list = ManualModel::where(['status' => 1, 'type_id' => $all_type_ids])
                ->field('id,title,route_name,cover')
                ->order('id desc')
                ->paginate(24);
        
        return $list;
    }

    /**
     * 简要
     */
    public function read()
    {
		$route_name = input('route_name/s');
        
    	if (empty($route_name)) $this->error('参数错误');
        $map['status'] = 1;
        if (!empty($route_name)) {
            $map['route_name'] = $route_name;
        }
        $minfo = ManualModel::where($map)->find();
        $docs  = ManualDocModel::where(['status' => 1, 'manual_id' => $minfo['id']])
                 ->field('id,doc_name')
                 ->order('id asc,sort asc')
                 ->select();

        $this->assign([
            'title' => $minfo['title'] . ' - ' . config('app.site_setting.web_site_title'),
            'minfo' => $minfo,
            'docs'  => $docs
        ]);
        return $this->fetch();
    }
    
    /**
     * 阅读
     */
    public function project()
    {
        $route_name = input('route_name/s');
        $manual_id  = ManualModel::where(['status' => 1, 'route_name' => $route_name])->value('id');
        if (empty($manual_id)) return $this->_empty();

        $doc_id = ManualDocModel::where(['manual_id' => $manual_id, 'status' => 1])->order('sort asc,id asc')->value('id');
        if (empty($doc_id)) return $this->_empty();

        $this->redirect(url('/manual/'.$route_name.'/'.$doc_id));
    }

    /**
     * 详细
     */
    public function content()
    {
        $route_name = $this->request->param('route_name');
        $doc_id     = $this->request->param('id');
        $minfo      = ManualModel::where(['status' => 1, 'route_name' => $route_name])->field('id,title')->find();
        if (empty($minfo)) return $this->_empty();

        $allDocs  = cache('doc_'.$route_name);
        $docsTree = cache('doc_'.$route_name.'_tree');
        if (!$allDocs) {
            $allDocs = ManualDocModel::where(['manual_id' => $minfo['id'], 'status' => 1])->order('sort asc')->column('id,pid,doc_name');
            $docsTree = list_to_tree_two($allDocs);
            if (config('app.site_setting.develop_mode') == 0) {
                cache('doc_'.$route_name, $allDocs);
                cache('doc_'.$route_name.'_tree', $docsTree);
            }
        }
        $location = get_docs_parents($allDocs, $doc_id);
        $treeHtml = proc_html($docsTree, $route_name, $location, 0);

        /* 当前文档信息 */
        $thisOne = ManualDocModel::where(['id' => $doc_id, 'status' => 1])->find();
        if (empty($thisOne)) return $this->_empty();

        ManualDocModel::where('id', $doc_id)->setInc('clicks');

        $prevOne = ManualDocModel::where('manual_id = ' .$thisOne['manual_id']. ' and id < '.$doc_id)->order('id desc')->field('id,doc_name')->find();
        $nextOne = ManualDocModel::where('manual_id = ' .$thisOne['manual_id']. ' and id > '.$doc_id)->order('id asc')->field('id,doc_name')->find();
        
        $this->assign([
            'title'       => $minfo['title'] . ' - ' . config('app.site_setting.web_site_title'),
            'route_name'  => $route_name,
            'prevOne'     => $prevOne,
            'nextOne'     => $nextOne,
            'treeHtml'    => $treeHtml,
            'info'        => $thisOne
        ]);

        return $this->fetch('project');
    }
}